/**
 * @Author 天下无敌
 * @Date 
 * @AIDE AIDE+ 
 */
package com.ecs.core.gsystem.chunk;
import com.artemis.systems.IteratingSystem;
import com.artemis.annotations.All;
import com.artemis.ComponentMapper;
import com.badlogic.gdx.utils.IntArray;
import com.ecs.core.EntityBuilder;
//import com.ecs.core.debug.Debug;
import com.ecs.core.components.map.ChunkCom;

@All({ChunkCom.class})
public class ChunkUnloadSystem extends IteratingSystem
{
	public static final String TAG = "ChunkUnloadSystem";
	private ComponentMapper<ChunkCom> chunks;
	private IntArray pendingDeleteS = new IntArray();
	
	@Override
	protected void process(int p)
	{
		ChunkCom c = chunks.get(p);
		if(c.isUnload && !pendingDeleteS.contains(p)){
			pendingDeleteS.add(p);
			//Debug.log(TAG,String.format("Block %d is added to the list to be uninstalled",p));
		}
	}

	@Override
	protected void end()
	{
		super.end();
		if(!pendingDeleteS.isEmpty()){
			int p = pendingDeleteS.removeIndex(0);
			removeChunk(p);
		}
	}

	public void removeChunk(int p){
		ChunkCom c = chunks.get(p);
		int[] ids = c.activeEntityids.getData();
		for(int i = 0;i < c.activeEntityids.size();i++){
			EntityBuilder.init(world,ids[i]).Delete(0);
		}
		EntityBuilder.init(world,p).Delete(0);
	}
	
}
